package com.manger.utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.awt.*;
import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelUtils {

    public static void updateRowBackgroundColor(String filePath, int rowIndex) {

        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis);
             FileOutputStream out = new FileOutputStream(filePath)) {

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
            Row row = sheet.getRow(rowIndex);
            // 设置行的背景颜色

            CellStyle style = workbook.createCellStyle();
            Font font = workbook.createFont();
            font.setColor(IndexedColors.WHITE.getIndex()); // 设置字体颜色为白色
            style.setFont(font); // 应用字体样式到单元格样式中
            style.setFillForegroundColor(IndexedColors.GREEN.getIndex()); // 设置填充颜色为黄色
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置填充模式为纯色
            row.setRowStyle(style); // 将样式应用到整行

            workbook.write(out); // 写入到输出文件
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
